﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Media;
using FarmerJoe.Helpers;
using FarmerJoe.Helpers.Logging;
using FarmerJoe.Settings;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.WoWInternals.WoWObjects;

namespace FarmerJoe
{
    public partial class Base : CombatRoutine
    {
        public override string Name
        {
            get
            {
                switch (Me.Class)
                {
                    case WoWClass.Hunter:
                        return string.Format("Fpsware Hunter: Beast Lord [{0}, {1}]", Version, VerDate);

                    case WoWClass.Druid:
                        return string.Format("Fpsware Druid: Shapeshifter [{0}, {1}]", Version, VerDate);

                    case WoWClass.Warlock:
                        return string.Format("Fpsware Warlock: Shadow Walker [{0}, {1}]", Version, VerDate);
                }

                return string.Format("Fpsware CC [{0}, {1}]", Version, VerDate);
            }
        }

        public override WoWClass Class
        {
            get
            {
                if (Me.Class == WoWClass.Druid) return WoWClass.Druid;
                if (Me.Class == WoWClass.Warlock) return WoWClass.Warlock;
                //if (Me.Class == WoWClass.Priest) return WoWClass.Priest;
                if (Me.Class == WoWClass.Hunter) return WoWClass.Hunter;
                
                return WoWClass.None;
            }
        }

        public override bool WantButton { get { return true; } }
        
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static WoWUnit CT{get{return !Me.GotTarget ? null : Me.CurrentTarget;}}

        private const string Version = "1.3.7";
        private const string VerDate = "6 Nov 2012";
        private const string Warning = "";

        private static string TagLine
        {
            get
            {
                List<string> tagLines;
                switch (Me.Class)
                {
                        case WoWClass.Druid:
                        tagLines = new List<string>
                                       {
                                           "In mother Russia, Fpsware Cat kills curiousity!",
                                           "I'm the kitty your mother warned you about",
                                           "Go ahead, pat me I dare you!",
                                           "I came with a warning label, but I chewed it off!",
                                           "GRRRRRRRRRRR......."
                                       };
                        break;

                        case WoWClass.Warlock:
                        tagLines = new List<string>
                                       {
                                           "Soul stone!? But I don't have a soul!",
                                           "Who dares to summon me?",
                                           "Come to the dark side Luke",
                                           "Embrace you dark side"
                                       };
                        break;

                        case WoWClass.Hunter:
                        tagLines = new List<string>
                                       {
                                           "I am a witty tag line",
                                       };
                        break;

                    default:
                        tagLines = new List<string> {"Witty tag line goes here"};
                        break;
                }


                Random random = new Random();
                int index = random.Next(tagLines.Count);
                string result = tagLines[index];

                return result;
            }
        }

        public override void Initialize()
        {
            Log.Write(Name);
            Log.Write(TagLine);
            Log.Write(" ");
            Log.Write(Color.FromArgb(255, 121, 221, 255), Warning);
        }

        public Base()
        {
            
            BotEvents.OnBotStarted += BotEvents_OnBotStarted;
            BotEvents.OnBotStopped += BotEvents_OnBotStopped;
        }

        private void BotEvents_OnBotStarted(System.EventArgs args)
        {
            if (!RoutineManager.Current.Name.Contains("Fpsware")) return;

            // Run this code when the START button is pressed
            Log.Write(LogLevel.Diagnostic, "Starting CC...");
            Timers.Add("Spell Cast Success");
            EventHandlers.AttachCombatLogEvent();
            
            // Make sure we populate out Glyphs now.
            TalentsAndGlyphs.Update();

            switch (Me.Class)
            {
                    case WoWClass.Warlock:
                    FpswareSettings.Instance.Warlock.Load();
                    break;

                    case WoWClass.Druid:
                    FpswareSettings.Instance.Druid.Load();
                    break;

                    case WoWClass.Hunter:
                    FpswareSettings.Instance.Hunter.Load();
                    break;

                    case WoWClass.Priest:
                    FpswareSettings.Instance.Priest.Load();
                    break;
            }
        }

        private void BotEvents_OnBotStopped(System.EventArgs args)
        {
            if (!RoutineManager.Current.Name.Contains("Fpsware")) return;

            // Run this code when the STOP button is pressed
            Log.Write(LogLevel.Diagnostic, "Stopping CC...");
            EventHandlers.DetachCombatLogEvent();
        }


        private static Thread Thread { get; set; }
        private uiForm1 _gui;

        public override void OnButtonPress()
        {
            Thread = new Thread(OpenGui) { IsBackground = true, Name = "GuiThread" }; Thread.Start();
        }
        public void OpenGui() { _gui = new uiForm1(); _gui.ShowDialog(); }

    }
}
